package com.dongdong.bookstore.module.pms.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dongdong.bookstore.common.api.CommonPage;
import com.dongdong.bookstore.common.exception.Asserts;
import com.dongdong.bookstore.module.pms.model.Subject;
import com.dongdong.bookstore.module.pms.service.SubjectService;
import io.swagger.annotations.Api;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import java.util.List;

/**
 * <p>
 * 科目 前端控制器
 * </p>
 *
 * @author chiangkai
 * @since 2021-10-31
 */
@Api(tags = "科目")
@RestController
public class SubjectController {

    @Autowired
    private SubjectService subjectService;

    @PostMapping("/pms/subject/create")
    public String create(
            @RequestParam("name") @Validated @NotEmpty(message = "科目名称不能为空") String name,
            @RequestParam(value = "name", defaultValue = "0") Integer sort
    ) {
        Subject subject = new Subject();
        subject.setName(name);
        subject.setSort(sort);
        Asserts.isFalse(subjectService.save(subject), "添加失败");
        return "添加成功";
    }

    @GetMapping("/pms/subject/delete/{id}")
    public String delete(@PathVariable Long id) {
        Asserts.isFalse(subjectService.removeById(id), "删除失败");
        return "删除成功";
    }

    @PostMapping("/pms/subject/update/{id}")
    public String update(
            @PathVariable Long id, @RequestParam("name") @Validated @NotEmpty(message = "科目名称不能为空") String name,
            @RequestParam(value = "name", defaultValue = "0") Integer sort
    ) {
        Subject subject = subjectService.getById(id);
        Asserts.isNull(subject, "更新的科目不存在");
        subject.setName(name);
        subject.setSort(sort);
        Asserts.isFalse(subjectService.updateById(subject), "更新失败");
        return "更新成功";
    }

    @GetMapping("/p/pms/subject/list")
    public CommonPage<Subject> list(
            @RequestParam(value = "keyword", required = false) String keyword,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum
    ) {
        QueryWrapper<Subject> wrapper = new QueryWrapper<>();
        if (StrUtil.isNotEmpty(keyword)) {
            wrapper.lambda().like(Subject::getName, keyword);
        }
        wrapper.lambda().orderByDesc(Subject::getSort);
        Page<Subject> page = subjectService.page(new Page<>(pageNum, pageSize), wrapper);
        return CommonPage.restPage(page);
    }

    @GetMapping("/p/pms/subject/listAll")
    public List<Subject> listAll() {
        return subjectService.list(new QueryWrapper<Subject>().lambda().orderByDesc(Subject::getSort));
    }
}
